import utils from '@common/utils/utils';

/**
 * 运费计算
 */
const freightUtils = {
    /**
     * 计算运费
     * @param total_weight 物品总重量
     * @param expressOrg 物流公司信息
     * @returns {number}
     */
    calcFreight(total_weight, expressOrg) {
        let freight = 0;
        console.log(total_weight, expressOrg);
        if (typeof total_weight === 'string') {
            total_weight = Number(total_weight);
        }
        if (total_weight > 0) {
            if (utils.isNotEmptyList(expressOrg.item_list)) {
                //判断重量适用的规则（根据起始重量和终止重量）
                let filter = expressOrg.item_list.filter(item => {
                    let isMax = utils.isNotEmpty(item.max_value) ? total_weight <= Number(item.max_value) : true;
                    return total_weight > Number(item.min_value) && isMax;
                });
                if (utils.isNotEmptyList(filter)) {
                    //找到符合条件的,根据规则计算运费
                    freight = this.getFreight(total_weight, filter[0], expressOrg.rate_list);
                } else {
                    let has = false;
                    //没有找到符合条件的规则，则判断是否处于某2个规则之间，如果处于，则使用后一个规则计算
                    let sortedList = expressOrg.item_list.sort((a, b) => Number(a.min_value) - Number(b.min_value));
                    for (let i = 0; i < sortedList.length; i++) {
                        const item = sortedList[i];
                        if (i + 1 < sortedList.length) {
                            const nextItem = expressOrg.item_list[i + 1];
                            if (total_weight > Number(item.max_value) && total_weight <= Number(nextItem.min_value)) {
                                freight = this.getFreight(nextItem.min_value, nextItem, expressOrg.rate_list);
                                has = true;
                                break
                            }
                        }
                    }
                    if (!has){
                        const nextItem = sortedList[sortedList.length-1];
                        freight = this.getFreight(nextItem.max_value, nextItem, expressOrg.rate_list);
                    }
                }
            }
        }
        return freight;
    },
    /**
     * 基于某个规则计算运费
     * @param weight 物品重量
     * @param rule_item 计费规则
     * @param rate_list  费率表
     * @returns {number}
     */
    getFreight(weight, rule_item, rate_list) {
        //获取运费
        // 先计算计费重量，计费重量= (实际重量+(包材重量不为空?包材重量:0)/(包材系数不为空?包材系数:1),相除采用向上取整
        // 再计算运费，1）按单价则运费=计费重量*单价，2）固定运费，则运费=固定运费，3）查费率表，将计费重量考虑重量梯度，得到处理的重量，再查费率表得到对应重量的运费
        let freight = 0;
        if (utils.isNotEmpty(rule_item.package_weight)) {
            weight = utils.accAdd(weight, rule_item.package_weight);
        }
        if (utils.isNotEmpty(rule_item.package_ratio)) {
            weight = Math.ceil(utils.accDiv(weight, rule_item.package_ratio));
        }
        if (rule_item.valuation_mode === 'price') {
            freight = utils.accMul(rule_item.price, weight);
        } else if (rule_item.valuation_mode === 'freight') {
            freight = rule_item.freight;
        } else {
            let weightInt = Math.ceil(utils.accDiv(weight, rule_item.weight_grad));
            let calcWeight = utils.accMul(weightInt, rule_item.weight_grad);
            if (utils.isNotEmptyList(rate_list)) {
                debugger
                let filter = rate_list.filter(t => Number(t.weight) === Number(calcWeight));
                if (utils.isNotEmptyList(filter)) {
                    freight = filter[0].freight;
                }
            }
        }
        return freight;
    },
};
export default freightUtils;
